/**
 * FriendRelationImpl.java
 */
package com.tobeface.sns.infrastructure.persist.pal.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import com.google.common.collect.Lists;
import com.kissme.core.orm.Page;
import com.tobeface.sns.domain.User;
import com.tobeface.sns.domain.relationship.UserRelationship;
import com.tobeface.sns.infrastructure.persist.CrudMongoRepositorySupport;
import com.tobeface.sns.infrastructure.persist.pal.FriendRelationRepository;

/**
 * @author Dylan
 * @time 2012-7-30
 */
@Repository
public class FriendRelationImpl extends CrudMongoRepositorySupport<UserRelationship, String> implements FriendRelationRepository {

	/**
	 * @param mongoOperations
	 */
	@Autowired
	protected FriendRelationImpl(MongoOperations mongoOperations) {
		super(mongoOperations);
	}

	/* (non-Javadoc)
	 * @see com.sinzv.sns.infrastructure.persist.pal.FriendRelationRepository#attentionFriend(com.sinzv.sns.domain.User, com.sinzv.sns.domain.User)
	 */
	@Override
	public void attentionFriend(User self, User friend) {
		UserRelationship relation = new UserRelationship(self, friend);
		save(relation);
	}

	/* (non-Javadoc)
	 * @see com.sinzv.sns.infrastructure.persist.pal.FriendRelationRepository#cancelAttention(com.sinzv.sns.domain.User, com.sinzv.sns.domain.User)
	 */
	@Override
	public void cancelAttention(User self, User friend) {
		UserRelationship relation = find(self, friend);
		delete(relation);
	}

	/* (non-Javadoc)
	 * @see com.sinzv.sns.infrastructure.persist.pal.FriendRelationRepository#attentionWho(com.sinzv.sns.domain.User)
	 */
	@Override
	public Page<User> attentionWho(Page<User> page,User self) {
		List<UserRelationship> result = getMongoOperations().find(new Query(Criteria.where("sender").is(self)), UserRelationship.class);
		List<User> user = Lists.newArrayList();
		for(UserRelationship relation : result){
			user.add(relation.getReceiver());
		}
		page.setResult(user);
		return page;
	}

	/* (non-Javadoc)
	 * @see com.sinzv.sns.infrastructure.persist.pal.FriendRelationRepository#whoAttention(com.sinzv.sns.domain.User)
	 */
	@Override
	public Page<User> whoAttention(Page<User> page,User self) {
		List<UserRelationship> result = getMongoOperations().find(new Query(Criteria.where("receiver").is(self)), UserRelationship.class);
		List<User> user = Lists.newArrayList();
		for(UserRelationship relation : result){
			user.add(relation.getSender());
		}
		page.setResult(user);
		return page;
	}
	
	public UserRelationship find(User self, User friend){
		UserRelationship relation = getMongoOperations().findOne(new Query(Criteria.where("sender").is(self).andOperator(Criteria.where("receiver").is(friend))), UserRelationship.class);
		return relation;
	}

}
